package com.jpatest.demo.Service;

import com.jpatest.demo.Redis.UserRedis;
import com.jpatest.demo.Repository.UserRepository;
import com.jpatest.demo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.NoSuchElementException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;

@Service
public class UserServiceimpl {
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private UserRedis userRedis;
    @Autowired
    private ExecutorService executorService;

    private static final String keyHead = "mysql:get:user:";

    @Cacheable(cacheNames = "jpauser",key = "#id")
    public User findById(Long id){
        User user = null;
        try{
            user= userRepository.findById(id).get();
        }catch (NoSuchElementException e){
            System.out.println("该ID没有对应的用户！！！");
            user = null;
            return user;
        }
        return user;
    }

    public List<User> findAll(){
        List<User> users = userRepository.findAll();
        return  users;
    }

    @CachePut(cacheNames = "jpauser",key = "#user.id")
    public User create(User user){
        user = userRepository.save(user);
        return user;
    }

    @CachePut(cacheNames = "jpauser",key = "#user.id")
    public User update(User user){
        return userRepository.save(user);
    }

    @CacheEvict(cacheNames = "jpauser",key = "#id")
    public void delete(Long id){
        userRepository.deleteById(id);
    }

    public void testThreadPool(){
        ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor)executorService;
        for(int i=0;i<20;i++){
            MyTask myTask = new MyTask(i);
            executorService.execute(myTask);
            System.out.println("线程池中线程数目："+threadPoolExecutor.getPoolSize()+"，队列中等待执行的任务数目："+
                    threadPoolExecutor.getQueue().size()+"，已执行玩别的任务数目："+threadPoolExecutor.getCompletedTaskCount());
        }
        threadPoolExecutor.shutdown();
    }

    class MyTask implements Runnable {
        private int taskNum;

        public MyTask(int num) {
            this.taskNum = num;
        }

        @Override
        public void run() {
            System.out.println("正在执行task "+taskNum);
            try {
                Thread.currentThread().sleep(4000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor)executorService;
            System.out.println("线程池中线程数目："+threadPoolExecutor.getPoolSize()+"，队列中等待执行的任务数目："+
                    threadPoolExecutor.getQueue().size()+"，已执行玩别的任务数目："+threadPoolExecutor.getCompletedTaskCount());
            System.out.println("task "+taskNum+"执行完毕");
        }
    }
}
